package cmd

import (
	"os"

	"github.com/spf13/cobra"

	"testkjcld.bhecard.com/gitlab/kit/esim/infra"
	filedir "testkjcld.bhecard.com/gitlab/kit/esim/pkg/file-dir"
	"testkjcld.bhecard.com/gitlab/kit/esim/pkg/templates"
	"testkjcld.bhecard.com/gitlab/kit/esim/pkg/tool"
	"testkjcld.bhecard.com/gitlab/kit/esim/tool/db2entity"
	domainfile "testkjcld.bhecard.com/gitlab/kit/esim/tool/db2entity/domain-file"
)

var ckdb2entityCmd = &cobra.Command{
	Use:   "ckdb2entity",
	Short: "clickhouse将对应的表生成实体(infra 需手动添加clickhouse client, 调用wire gen命令)",
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
		// 同名外部参数会被db2entity自命令修改代替, 因此使用varxxp 形式进行值的本地重写
		v.Set("host", ckHost)
		v.Set("port", ckPort)
		v.Set("table", ckTable)
		v.Set("database", ckDatabase)
		v.Set("user", ckUser)
		v.Set("password", ckPasswd)
		v.Set("boubctx", ckBoubCtx)
		v.Set("package", ckPackage)
		v.Set("struct", ckStruct)
		v.Set("gorm", ckGorm)
		v.Set("entity_target", ckEntityTarget)
		v.Set("disable_entity", ckDisableEntity)
		v.Set("dao_target", ckDaoTarget)
		v.Set("isable_dao", ckDisableDao)
		v.Set("repo_target", ckRepoTarget)
		v.Set("disable_repo", ckDisableRepo)

		dbConf := domainfile.NewDbConfig()
		dbConf.ParseConfig(v, logger)

		// select table's columns from db
		columnsInter := domainfile.NewCkDbColumnsInter(logger)
		tpl := templates.NewTextTpl()

		daoDomainFile := domainfile.NewDaoDomainFile(
			domainfile.WithDaoDomainFileLogger(logger),
			domainfile.WithDaoDomainFileTpl(tpl),
			domainfile.WithDaoDbType("clickhouse"),
		)

		entityDomainFile := domainfile.NewEntityDomainFile(
			domainfile.WithEntityDomainFileLogger(logger),
			domainfile.WithEntityDomainFileTpl(tpl),
			domainfile.WithEntityDbType("clickhouse"),
		)

		repoDomainFile := domainfile.NewRepoDomainFile(
			domainfile.WithRepoDomainFileLogger(logger),
			domainfile.WithRepoDomainFileTpl(tpl),
		)

		writer := filedir.NewEsimWriter()

		shareInfo := domainfile.NewShareInfo()
		shareInfo.DbConf = dbConf

		db2EntityOptions := db2entity.Db2EnOptions{}
		db2Entity := db2entity.NewDb2Entity(
			db2EntityOptions.WithLogger(logger),
			db2EntityOptions.WithDbConf(dbConf),
			db2EntityOptions.WithColumnsInter(columnsInter),
			db2EntityOptions.WithWriter(writer),
			db2EntityOptions.WithShareInfo(shareInfo),

			db2EntityOptions.WithExecer(tool.NewCmdExec()),
			db2EntityOptions.WithDomainFile(entityDomainFile, daoDomainFile, repoDomainFile),
			db2EntityOptions.WithInfraer(infra.NewInfraer(
				infra.WithIfacerLogger(logger),
				infra.WithIfacerWriter(writer),
				infra.WithIfacerExecer(tool.NewCmdExec()),
			)),
			db2EntityOptions.WithDbType("clickhouse"),
		)

		err := db2Entity.Run(v)
		if err != nil {
			logger.Errorf(err.Error())
		}
	},
}

var (
	ckHost     string
	ckPort     string
	ckUser     string
	ckPasswd   string
	ckDatabase string
	ckTable    string

	ckBoubCtx       string
	ckPackage       string
	ckStruct        string
	ckGorm          bool
	ckEntityTarget  string
	ckDisableEntity bool
	ckDaoTarget     string
	ckDisableDao    bool
	ckRepoTarget    string
	ckDisableRepo   bool
)

func init() {
	rootCmd.AddCommand(ckdb2entityCmd)

	// ckdb2entityCmd.Flag()
	ckdb2entityCmd.Flags().StringVarP(&ckHost, "host", "H", os.Getenv("ESIM_CKDB_HOST"), "数据库 host")

	ckdb2entityCmd.Flags().StringVarP(&ckPort, "port", "P", os.Getenv("ESIM_CKDB_PORT"), "数据库 port")

	ckdb2entityCmd.Flags().StringVarP(&ckTable, "table", "t", "", "数据表表")

	ckdb2entityCmd.Flags().StringVarP(&ckDatabase, "database", "d", "", "数据库名")

	ckdb2entityCmd.Flags().StringVarP(&ckUser, "user", "u", os.Getenv("ESIM_CKDB_USER"), "链接数据库的用户")

	ckdb2entityCmd.Flags().StringVarP(&ckPasswd, "password", "p", os.Getenv("ESIM_CKDB_PASSWORD"), "链接数据库的密码")

	ckdb2entityCmd.Flags().StringVarP(&ckBoubCtx, "boubctx", "b", "", "用于界限上下文")

	ckdb2entityCmd.Flags().StringVarP(&ckPackage, "package", "", "", "包名，空使用数据表名")

	ckdb2entityCmd.Flags().StringVarP(&ckStruct, "struct", "s", "", "实体的名称，空使用数据表名")

	ckdb2entityCmd.Flags().BoolVarP(&ckGorm, "gorm", "g", true, "增加grom注解")

	ckdb2entityCmd.Flags().StringVarP(&ckEntityTarget, "entity_target", "", "", "实体保存的路径")

	ckdb2entityCmd.Flags().BoolVarP(&ckDisableEntity, "disable_entity", "", false, "不生成实体")

	ckdb2entityCmd.Flags().StringVarP(&ckDaoTarget, "dao_target", "", "internal/infra/dao", "DAO存放路径")

	ckdb2entityCmd.Flags().BoolVarP(&ckDisableDao, "disable_dao", "", false, "关闭DAO的生成")

	ckdb2entityCmd.Flags().StringVarP(&ckRepoTarget, "repo_target", "", "internal/infra/repo", "repo的存放路径")

	ckdb2entityCmd.Flags().BoolVarP(&ckDisableRepo, "disable_repo", "", false, "关闭repo的生成")

	// err := v.BindPFlags(ckdb2entityCmd.Flags())
	// if err != nil {
	// 	logger.Errorf(err.Error())
	// }
}
